//引入当前文件对应的less文件
require("../less/loginID.less");
document.ready(function () {

    // 获取元素
    const inputs = document.querySelectorAll(".inputs");//输入框
    // const authCode = document.querySelector(".auth-code");//验证码
    const hint = document.querySelector(".hint");//提示
    const login = document.querySelector(".login");//切换到注册

    //    添加点击事件
    inputs[2].addEventListener("click", function () {
        // 非空验证
        for (let i = 0; i < inputs.length - 1; i++) {
            if (inputs[i].value == "") {
                hint.textContent = `请输入完整的账号信息`;
                return
            } else {
                hint.textContent = ``;

                if (hint.textContent == "") {
                    // 验证手机号
                    let sjhyz = /^1[3-9]\d{9}$/;
                    inputs[0].value
                    if (!sjhyz.test(inputs[0].value)) {
                        hint.textContent = `请输入正确的手机号`;
                    } else {
                        //如果手机号正常就验证密码
                        hint.textContent = ``;
                        let passw = /^[a-zA-Z]\w{5,17}$/
                        if (!passw.test(inputs[1].value)) {
                            hint.textContent = `密码格式错误`;
                        } else {
                            hint.textContent = ``;
                            // 
                            //    创建ajax
                            axios.post("http://139.9.177.51:3701/api/user/login", {
                                    "account":inputs[0].value,"password":inputs[1].value
                                    
                            }).then(function(res){
                               
                                let re=res.data;
                                console.log(res.data.errno);
                                if(res.data.errno==0){
                                 let rse=JSON.stringify({
                                    "account":inputs[0].value,
                                    "password":inputs[1].value
                                 });
                                 console.log("aaaaaaaaa",rse)
                                localStorage.setItem("user" ,rse)
                                location.href="index.html"
                                }
                            }).catch((err) => {
                                console.log(err);
                            })
                        }
                    }
                }


            }
        }
    });
    //  添加点击事件
    login.addEventListener("click", function () {
        location.href = "register.html"//切换到注册
    })
})